#include "Mydelay.h"

volatile uint16_t delay_time=0;

void Delay_1ms(uint ms)
{
	 delay_time=0;
    for(int i=1;i<=1000;i++)
    {
    		while(delay_time<ms)
    	{
    			
    	}
    	delay_time=0;
  
    }
}

void Delay_1us(uint us)
{
		delay_time=0;
  		while(delay_time<us)
		{
				
		}
		delay_time=0;
}

void Delay_Init(void)
{
	NVIC_InitType NVIC_InitStructure;
	TMR_TimerBaseInitType  TMR_TMReBaseStructure;
  /* Enable the TMR2 global Interrupt */
  NVIC_InitStructure.NVIC_IRQChannel = TMR6_GLOBAL_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

  NVIC_Init(&NVIC_InitStructure);

 	RCC_APB1PeriphClockCmd(RCC_APB1PERIPH_TMR6, ENABLE);
 // PrescalerValue = (uint16_t) (SystemCoreClock / 12000000) - 1;

  /* TMRe base configuration */
  TMR_TimeBaseStructInit(&TMR_TMReBaseStructure);
  TMR_TMReBaseStructure.TMR_Period = 65535;
  TMR_TMReBaseStructure.TMR_DIV = 0;
  TMR_TMReBaseStructure.TMR_ClockDivision = 0;
  TMR_TMReBaseStructure.TMR_CounterMode = TMR_CounterDIR_Up;

  TMR_TimeBaseInit(TMR6, &TMR_TMReBaseStructure);
  TMR_INTConfig(TMR6,TMR_INT_Overflow , ENABLE);

  /* TMR2 enable counter */
  TMR_Cmd(TMR6, ENABLE);
}
